import time

from selenium.webdriver.support.select import Select

from webAutomation.utils import DriverUtil
import logging
from selenium.webdriver.support.wait import WebDriverWait


# 对象层
class BasePage:

    def __init__(self):
        # 获取浏览器驱动对象
        self.driver = DriverUtil.get_driver()

    def find_element(self, location, timeout=10, poll=0.5):
        logging.info("location={}".format(location))
        time.sleep(2)
        # return self.driver.find_element(location[0],location[1])
        return WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll).until(
            lambda x: x.find_element(*location))

    def find_elements(self, location, timeout=10, poll=0.5):
        logging.info("location={}".format(location))
        # time.sleep(2)
        # return self.driver.find_element(location[0],location[1])
        return WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll).until(
            lambda x: x.find_element(*location))


# 操作层
class BaseHandle:
    def input_text(self, element, text):
        # 清空输入框
        element.clear()
        # 输入文本
        element.send_keys(text)

    # 下拉列表工具
    def select_tools(self, element, text):
        'element为元素对象'
        select = Select(element)
        select.select_by_visible_text(text)
